
#include "flagmenulayer.h"
#include "resourcemanager.h"
#include "gamemanager.h"
#include "messagequeue.h"

static const Point ARR_ICON_POS[] = {Point(40,160), Point(160,160), Point(40,55), Point(160,55), };

FlagMenuLayer::FlagMenuLayer(): 
	_isAnimating(false)
	, soldierType(SoldierType::count)
	, _menuBg(nullptr)
{

}

FlagMenuLayer::~FlagMenuLayer() {

}

bool FlagMenuLayer::init() {
	if ( !Layer::init() ){
		return false;
	}

	_menuBg = CREATE_SPRITE("textures/ui/flag_menu_bg.png");
	_menuBg->setScale(GM->minscale * 0.2f);
	this->addChild(_menuBg);

	for(int i = 0; i < (int)SoldierType::count; i++) {
		sprintf(buf, "textures/ui/%s.png", SOLDIER_TYPE_NAMES[i]);
		Sprite* unitSprite = CREATE_SPRITE(buf);
		unitSprite->setScale(1.0f);
		unitSprite->setPosition(ARR_ICON_POS[i]);
		_menuBg->addChild(unitSprite);

		// TODO: addPriceTag(unitSprite, 100);
		addPriceTag(unitSprite, 100);

		_unitIcons[i] = unitSprite;
	}

	scheduleUpdate();

	return true;
}

void FlagMenuLayer::showLayer(const Point& pos) {
	if(!_isAnimating) {
		_isAnimating = true;

		_menuBg->setPosition(SCALE_PT(pos));

		_menuBg->runAction( Sequence::create(
			ScaleTo::create(0.15f, GM->minscale),
			CallFunc::create(CC_CALLBACK_0(FlagMenuLayer::onLayerAnimationFinished, this)),
			nullptr)
			);

		soldierType = SoldierType::count;
	}
}

void FlagMenuLayer::closeLayer() {
	if(!_isAnimating) {
		_isAnimating = true;

		_menuBg->runAction( Sequence::create(
			ScaleTo::create(0.15f, GM->minscale * 0.2f),
			CallFunc::create(CC_CALLBACK_0(FlagMenuLayer::onLayerAnimationFinished, this)),
			nullptr)
			);
	}
}

void FlagMenuLayer::onLayerAnimationFinished(){
	setVisible(!isVisible());
	_isAnimating = false;
}

void FlagMenuLayer::addPriceTag(Node* node, int price) {
	Sprite* bg = CREATE_SPRITE("textures/ui/price_pad.png");
	bg->setScale(1.0f);

	bg->setPosition(Point(node->getContentSize().width/2.0f, 1.0f));
	bg->setAnchorPoint(Point(0.5f, 1.0f));
	node->addChild(bg);

	sprintf(buf, "%d", price);
	LabelBMFont* label = RM->createButtonLabelBMFont(buf, bg);
	label->setScale(0.7f);
	label->setColor(Color3B(255,255,0));
	label->setPosition(bg->getContentSize().width/2, bg->getContentSize().height/2);

}

SoldierType FlagMenuLayer::checkTouch( const Point& pt )
{
	SoldierType ret = SoldierType::count;

	Point localPt = _menuBg->convertToNodeSpace(pt);
	for(int i = 0; i < (int)SoldierType::count; i++) {
		Sprite* icon = _unitIcons[i];
		if(icon->getBoundingBox().containsPoint(localPt)) {
			ret = (SoldierType)i;
			break;
		}
	}

	return ret;
}

bool FlagMenuLayer::ccTouchBegan( Touch *touch, Event *event )
{
	bool ret = false;

	if(_isAnimating) {
		return ret;
	}

	SoldierType type = checkTouch(touch->getLocation());
	ret = (type != SoldierType::count);

	return ret;
}

void FlagMenuLayer::ccTouchMoved( Touch *touch, Event *event )
{
	if(_isAnimating) {
		return;
	}
}

void FlagMenuLayer::ccTouchEnded( Touch *touch, Event *event )
{
	if(_isAnimating) {
		return;
	}

	Point pt = touch->getLocation();
	float distance = pt.getDistance(touch->getStartLocation());
	if(distance <= GM->minscale * MOVEMENT_THRESHOLD) {
		soldierType = checkTouch(pt);
	}
}

void FlagMenuLayer::ccTouchCancelled( Touch *touch, Event *event )
{
	if(_isAnimating) {
		return;
	}
}

void FlagMenuLayer::update( float ticks )
{
	if(!this->isVisible()) {
		return;
	}

	handleMessage();
}

void FlagMenuLayer::handleMessage() {
	
}








